<?php
declare (strict_types = 1);

namespace app\middleware;

use app\model\MyRedis;

class FilterDuplicateRequestMiddleware
{
    public function handle(\think\Request $request, \Closure $next)
    {
        $key              = md5($request->ip());
        $filter_timestamp = $request->param('filter_timestamp');
        // 参数校验
        if (is_numeric($filter_timestamp) === false) return json(['code' => 0, 'message' => 'filter_timestamp 错误']);

        $redis = MyRedis::getRedisInstance();
        if ($redis->exists($key) && (int)$redis->get($key) >= (int)$filter_timestamp) return json(['code' => 0, 'message' => 'filter_timestamp 无效']);
        $redis->setex($key, 10, $filter_timestamp);

        return $next($request);
    }
}
